package keeto.com.vn.classified.adapter;

import java.util.Vector;

import keeto.com.vn.classfied.model.Category;
import keeto.com.vn.classified.common.ICategoriesProductsAdapter;
import keeto.com.vn.classified.ui.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SubCategoryAdapter extends BaseAdapter implements ICategoriesProductsAdapter{
	public static final String ACTION_SELECT_SEARCH = "SelectForSearch";
	public static final String ACTION_SPECIAL = "SelectAllSpecial";
	public static final String CMD_SEARCH_PRODUCT = "SelectForSearchProduct";

	private Vector<Category> dataSource = null;
	private String whatAction;
	private Context context;
	

	public SubCategoryAdapter(Vector<Category> dataSource, String whatAction, Context context) {
		this.dataSource = dataSource;
		this.whatAction = whatAction;
		this.context = context;
	}

	@Override
	public int getCount() {
		if (dataSource == null || dataSource.size() == 0) {
			return 0;
		} else {
			return dataSource.size();
		}
	}

	@Override
	public Object getItem(int position) {
		return dataSource.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (dataSource == null || dataSource.size() == 0) {
			return null;
		} else {
			String childCategoryName = dataSource.get(position).getName();

			View view = LayoutInflater.from(context).inflate(R.layout.ia_subcategory_item_view,
					parent, false);
			ViewHolder holder = new ViewHolder();
			holder.tvCategaryName = (TextView) view.findViewById(R.id.tv_name);
			if (whatAction
					.equals(CMD_SEARCH_PRODUCT)
					|| whatAction
							.equals(ACTION_SPECIAL)) {
				int count = dataSource.get(position).getCount();
				holder.tvCategaryName.setText(childCategoryName + "("
						+ String.valueOf(count) + ")");
			} else {
				holder.tvCategaryName.setText("" + childCategoryName);
			}
			return view;
		}
	}

	class ViewHolder {
		private TextView tvCategaryName;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setDataSource(Vector<?> dataSource) {
		this.dataSource = (Vector<Category>) dataSource;
	}
}
